<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .all {
            display: flex;
            text-align: center;

        }
    </style>
</head>

<body>
    <div id="app">
        <h2>{{ valx }}</h2>
        <my-cpn :material="arr" @btn="get"></my-cpn>
    </div>
    <script src="./js/vue.js"></script>
    <template id="myCpn">
        <div class="all">
            <div v-for="(item,index) in material" @click="btn(index)" :key="index">
                <!-- 照片 -->
                <div class="">
                    <img :src="(`${ item.img }`)" alt="">
                </div>
                <!-- 文字 -->
                <div class="">
                    <p>{{ item.name }}</p>
                    <p>{{ item.cpu }}</p>
                    <p>{{ item.price }}</p>
                </div>
            </div>
        </div>

    </template>
    <script>
        const app = new Vue({
            el: '#app',
            data() {
                return {
                    valx:"黑鲨4 Pro 骁龙888",
                    img:"",
                    arr: [{
                            img: `./img/1.jpg`,
                            name: "黑鲨4 Pro 骁龙888",
                            cpu: "cup型号 骁龙888",
                            parice: "￥3999起"
                        },
                        {
                            img: `./img/2.jpg`,
                            name: "小米12 Pro",
                            cpu: "cup型号 骁龙888",
                            parice: "￥5000起"
                        },
                        {
                            img: `./img/3.jpg`,
                            name: "小米11 Pro",
                            cpu: "cup型号 骁龙888",
                            parice: "￥4500起"
                        },
                        {
                            img: `./img/4.jpg`,
                            name: "iphone13 Pro",
                            cpu: "cup型号 骁龙888",
                            parice: "￥5000起"
                        },
                    ]
                }
            },
            components: {
                'my-cpn': {
                    template: '#myCpn',
                    data() {
                        return {
                            img:"",
                        }
                    },
                    props: ['material'],
                    methods: {
                        // add() {
                        //     // props拿到的数据不可改变
                        //     // 所以我们需要一个新的值去接收它
                        //     let newCount = this.count
                        //     newCount++
                        //     // 改变了这个值，我们是不是应该把它告诉父组件
                        //     this.$emit('add', newCount)
                        // },
                        // del() {
                        //     let newCount = this.count
                        //     newCount--
                        //     this.$emit('del', newCount)
                        // },
                        btn(i) {
                            let newval = this.material[i].name;
                            this.$emit('btn', newval)
                        }
                    }
                }
            },
            methods: {
                get(val) {
                    this.valx = val;
                }
            }
        })
    </script>
</body>

</html>